package com.suli.multimedia.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.scenic.util.ResponseResult;
import com.suli.multimedia.entity.SalesQuantity;
import com.suli.multimedia.entity.ScenicTicket;
import com.suli.multimedia.entity.UserTicket;
import com.suli.multimedia.service.SalesQuantityService;
import com.suli.multimedia.service.ScenicService;
import com.suli.multimedia.service.ScenicTicketService;
import com.suli.multimedia.service.UserTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SuLi
 * @since 2022-04-04
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/userTicket")
public class UserTicketController {

    @Autowired
    UserTicketService userTicketService;
    @Autowired
    ScenicService scenicService;
    @Autowired
    ScenicTicketService ticketService;
    @Autowired
    SalesQuantityService salesQuantityService;


    /**
     * 分页获取
     * @param status 门票状态
     * @param userId 用户id
     * @param limit 数量
     * @param page 页码
     * @return 门票列表
     */
    @GetMapping("/getTicketListByUserId/{userId}/{status}/{page}/{limit}")
    public ResponseResult getTicketListByUserId(@PathVariable String status,
                                                @PathVariable String userId,
                                                @PathVariable Long limit,
                                                @PathVariable Long page){
        Map<String, Object> userTicketList = userTicketService.getUserTicketList(status, userId, limit, page);
        if(ObjectUtils.isEmpty(userTicketList)){
            return ResponseResult.error().message("未获取到数据");
        }
        return ResponseResult.ok().data(userTicketList);
    }

    /**
     * 根据订单号，获取门票详情
     * @param orderId 门派订单号
     * @return 门票详情
     */
    @GetMapping("/getTicketDetail/{orderId}")
    public ResponseResult getTicketDetail(@PathVariable String orderId){
        UserTicket userTicket = userTicketService.getById(orderId);
        if(ObjectUtils.isEmpty(userTicket))
        return ResponseResult.error().message("获取门票详情失败，门票不存在");
        return ResponseResult.ok().data("ticketDetail",userTicket);
    }


    /**
     * 修改门票状态 USING->USED | UN_USE(expire< now())->EXPIRE
     * @param ticketId 门票id
     * @param status 门票状态
     * @return 修改结果
     */
    @PutMapping("/updateStatus/{ticketId}/{status}")
    public ResponseResult updateStatus(@PathVariable String ticketId,
                                       @PathVariable String status){
        userTicketService.updateStatus(ticketId, status);
        return ResponseResult.ok().message("检票成功，可以进入");
    }

    /**
     * 校准门票
     * @return 校准结果
     */
    @GetMapping("/calibrate")
    public ResponseResult calibrateStatus(){
        UpdateWrapper<UserTicket> updateWrapper=new UpdateWrapper<>();
        //"ticket_status=1 where ticket_status=3"
        updateWrapper.set("ticket_status",1).eq("ticket_status", 3).le("ticket_expire_time", new Date());
        userTicketService.update(updateWrapper);
        UpdateWrapper<UserTicket> updateWrapper1=new UpdateWrapper<>();
        updateWrapper1.set("ticket_status",2).eq("ticket_status",0).lt("ticket_expire_time", new Date());
        userTicketService.update(updateWrapper1);
        UpdateWrapper<ScenicTicket> updateWrapper2=new UpdateWrapper<>();
        updateWrapper2.set("scenic_ticket_status",1).le("scenic_ticket_expire", new Date());
        ticketService.update(updateWrapper2);
        QueryWrapper<SalesQuantity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("sales_volume", 0);
        salesQuantityService.remove(queryWrapper);
        return ResponseResult.ok().message("校准完成");
    }


    /**
     * 清理过期门票
     * @return 清理结果
     */
    @DeleteMapping("/deleteTicket")
    public ResponseResult deleteTicketExpire(){
        scenicService.deleteTicket();
        return ResponseResult.ok().message("清理完成");
    }

    /**
     * 退票
     * @param userTicket 待退门票
     */
    @DeleteMapping("/returnTicket")
    public ResponseResult returnRTicket(@RequestBody UserTicket userTicket){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(userTicket.getTicketBuyTime());
        try {
            Date parse = sdf.parse(format);
            userTicket.setTicketBuyTime(parse);
            if(userTicketService.returnTicketOne(userTicket)){
                return ResponseResult.ok().message("退票成功");
            }
            return ResponseResult.error().message("退票失败");
        } catch (ParseException e) {
            e.printStackTrace();
            return ResponseResult.error().message("退票失败");
        }

    }
}

